From d7f658426b06afb907f97ac3facf1acfd10ccc7e Mon Sep 17 00:00:00 2001
From: wwd <ericrock@foxmail.com>
Date: Tue, 24 May 2016 10:15:50 +0800
Subject: [PATCH] audio: able to handle situation without audio_pa_ctrl

---
 .../sound/soc/sunxi/audiocodec/sun8iw7_sndcodec.c  | 43 ++++++++++++++--------
 .../sun8iw7p1/configs/nanopi-h3/sys_config.fex     |  4 +-
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/sound/soc/sunxi/audiocodec/sun8iw7_sndcodec.c b/sound/soc/sunxi/audiocodec/sun8iw7_sndcodec.c
index 0deeea4..2cf2745 100755
--- a/sound/soc/sunxi/audiocodec/sun8iw7_sndcodec.c
+++ b/sound/soc/sunxi/audiocodec/sun8iw7_sndcodec.c
@@ -38,6 +38,7 @@
 static void  __iomem *baseaddr;
 static struct regulator* hp_ldo = NULL;
 static char *hp_ldo_str = NULL;
+static int has_audio_pa_ctrl = 0;
 
 /*for pa gpio ctrl*/
 static script_item_u item;
@@ -592,7 +593,8 @@ static void codec_init(void)
 	codec_wr_prcm_control(MIC2G_LINEOUT_CTR, 0x1, LINEOUTL_EN, 0x1);
 	codec_wr_prcm_control(MIC2G_LINEOUT_CTR, 0x1, LINEOUTR_EN, 0x1);
 	msleep(1250);
-	gpio_set_value(item.gpio.gpio, 1);
+	if (has_audio_pa_ctrl)
+		gpio_set_value(item.gpio.gpio, 1);
 	if (adcagc_used) {
 		adcagc_config();
 	}
@@ -1379,7 +1381,8 @@ static int codec_set_lineout(struct snd_kcontrol *kcontrol,
 		codec_wr_prcm_control(DAC_PA_SRC, 0x1, LMIXEN, 0x1);
 		codec_wr_prcm_control(DAC_PA_SRC, 0x1, RMIXEN, 0x1);
 		usleep_range(2000, 3000);
-		gpio_set_value(item.gpio.gpio, 1);
+		if (has_audio_pa_ctrl)
+			gpio_set_value(item.gpio.gpio, 1);
 		msleep(62);
 	} else {
 		codec_wr_prcm_control(LOMIXSC, 0x1, LMIXMUTELINEINL, 0x0);
@@ -1391,7 +1394,8 @@ static int codec_set_lineout(struct snd_kcontrol *kcontrol,
 
 			codec_wr_prcm_control(DAC_PA_SRC, 0x1, LMIXEN, 0x0);
 			codec_wr_prcm_control(DAC_PA_SRC, 0x1, RMIXEN, 0x0);
-			gpio_set_value(item.gpio.gpio, 0);
+			if (has_audio_pa_ctrl)
+				gpio_set_value(item.gpio.gpio, 0);
 		}
 	}
 
@@ -1532,7 +1536,8 @@ static int sndpcm_suspend(struct snd_soc_codec *codec)
 		pr_debug("In talking standby, audio codec do not suspend!!\n");
 		return 0;
 	}
-	gpio_set_value(item.gpio.gpio, 0);
+	if (has_audio_pa_ctrl)
+		gpio_set_value(item.gpio.gpio, 0);
 
 	codec_wr_prcm_control(MIC1G_MICBIAS_CTR, 0x1, MMICBIASEN, 0x0);
 
@@ -1754,17 +1759,21 @@ static int __init sndpcm_codec_probe(struct platform_device *pdev)
 	/*get the default pa val(close)*/
 	type = script_get_item("audio0", "audio_pa_ctrl", &item);
 	if (SCIRPT_ITEM_VALUE_TYPE_PIO != type) {
-		pr_err("script_get_item return type err\n");
-		return -EFAULT;
-	}
-	/*request gpio*/
-	req_status = gpio_request(item.gpio.gpio, NULL);
-	if (0 != req_status) {
-		pr_err("request gpio failed!\n");
-	}
-	gpio_direction_output(item.gpio.gpio, 1);
+		pr_info("script_get_item audio_pa_ctrl not found\n");
+		has_audio_pa_ctrl = 0;
+	} else {
+		/*request gpio*/
+		req_status = gpio_request(item.gpio.gpio, NULL);
+		if (0 != req_status) {
+			pr_err("request gpio failed!\n");
+			has_audio_pa_ctrl = 0;
+			return -EBUSY;
+		}
+		has_audio_pa_ctrl = 1;
+		gpio_direction_output(item.gpio.gpio, 1);
 
-	gpio_set_value(item.gpio.gpio, 0);
+		gpio_set_value(item.gpio.gpio, 0);
+	}
 	schedule_work(&codec_init_work);
 	snd_soc_register_codec(&pdev->dev, &soc_codec_dev_sndpcm, &sndpcm_dai, 1);
 
@@ -1800,6 +1809,9 @@ static int __exit sndpcm_codec_remove(struct platform_device *pdev)
 	sysfs_remove_group(&pdev->dev.kobj, &audio_debug_attr_group);
 
 	snd_soc_unregister_codec(&pdev->dev);
+
+	if (has_audio_pa_ctrl)
+		gpio_free(item.gpio.gpio);
 	return 0;
 }
 
@@ -1818,7 +1830,8 @@ static void sunxi_codec_shutdown(struct platform_device *devptr)
 	codec_wr_prcm_control(MIC1G_MICBIAS_CTR, 0x1, MMICBIASEN, 0x0);
 
 	//codec_wr_prcm_control(LINEOUT_VOLC, 0x1f, LINEOUTVOL, 0x0);
-	gpio_set_value(item.gpio.gpio, 0);
+	if (has_audio_pa_ctrl)
+		gpio_set_value(item.gpio.gpio, 0);
 
 	if ((NULL == codec_moduleclk)||(IS_ERR(codec_moduleclk))) {
 		pr_err("codec_moduleclk handle is invaled, just return\n");
